package comm;

import java.rmi.RemoteException;
import java.util.HashSet;

import org.apache.axis2.client.Options;
import org.apache.axis2.transport.http.HTTPConstants;

import client.UMessServiceStub;

// hnh20131215
// 短信邮件发送类
// 基于科大信息服务平台

// ??邮件群发时不能看见其他人

// 暂时仅支持单发，可支持群发
public class USTCMessageSender {

	private static final String UMessLoginID = PropertyManager.getProtery("UMessLoginID", null);
	private static final String UMessLoginPassword = PropertyManager.getProtery("UMessLoginPassword", null);
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		// debug
		HashSet<String> tolist = new HashSet<String>();
		tolist.clear();
		tolist.add("13739270351");
		sendSMS(tolist, "嘻嘻，你好");
		tolist.clear();
		tolist.add("keti_xuetangyi@163.com");
		tolist.add("hnh@mail.ustc.edu.cn");
		//sendEmail(tolist, "xixi-Test", "今天天气\n不错", "wo.@osla.com", "osla");
	}

	public static boolean sendSMS(HashSet<String> aPhoneNum, String aContent) {
		return messageSend(aPhoneNum, null, aContent, null, null, "sms", "mobile");
	}
	
	public static boolean sendEmail(HashSet<String> aToEmail, String aTitle, String aContent, String aFromEmail, String aFromName) {
		return messageSend(aToEmail, aTitle, aContent, aFromEmail, aFromName, "email", "email");
	}
	
	private static boolean messageSend(HashSet<String> aAdressTo, String aTitle, String aContent, String aFromAddress, String aFromName, String aSendTypeCode, String aReciverTypeCode) {
		String result;
		try {
			UMessServiceStub stub = new UMessServiceStub();
			// 初始化
			Options options = stub._getServiceClient().getOptions();
			options.setManageSession(true);
			options.setProperty("customCookieID", "PHPSESSID");
			options.setProperty(HTTPConstants.CHUNKED, false);
			stub._getServiceClient().setOptions(options);

			UMessServiceStub.WsClientSetCharset wsClientSetCharset = new UMessServiceStub.WsClientSetCharset();
			wsClientSetCharset.setCharset("UTF-8");
			wsClientSetCharset.setXmlCharset("UTF-8");
			result = stub.wsClientSetCharset(wsClientSetCharset).get_return();
			if(false == result.equals("1")) {
				Log.println("**设置字符集失败：" + result);
			}
			// 登录
			UMessServiceStub.WsCsLogin login = new UMessServiceStub.WsCsLogin();
			login.setCsLogin(UMessLoginID);
			login.setCsPassword(UMessLoginPassword);
			result = stub.wsCsLogin(login).get_return();
			if(false == result.equals("1")) {
				Log.println("**登录失败：" + result);
			}
			// 查询登录状态
			result = stub.wsCsCheckLogin(new UMessServiceStub.WsCsCheckLogin()).get_return();
			if(false == result.equals("1")) {
				Log.println("**未登录，登录状态：" + result);
				return false;
			}
			// 发送
			// // 创建消息
			UMessServiceStub.WsCreateMessage wsCreateMessage = new UMessServiceStub.WsCreateMessage();
			wsCreateMessage.setMessageContent(aContent);
			wsCreateMessage.setMessageContentFormat("plaintext");
			wsCreateMessage.setMessageFromAddress(aFromAddress);
			wsCreateMessage.setMessageFromName(aFromName);
			wsCreateMessage.setMessageTitle(aTitle);
			UMessServiceStub.WsCreateMessageResponse creatMessageResponse = stub.wsCreateMessage(wsCreateMessage);
			String messageId = creatMessageResponse.get_return();
			//System.out.println("创建消息messageId:" + messageId);
			// // 添加收件人
			java.util.Iterator<String> iter = aAdressTo.iterator();
			while (iter.hasNext()) {
				UMessServiceStub.WsMessageAddReceiver wsMessageAddReceiver = new UMessServiceStub.WsMessageAddReceiver();
				wsMessageAddReceiver.setMessageId(messageId);
				wsMessageAddReceiver.setMessagePriority("1");
				wsMessageAddReceiver.setReceiver(iter.next());
				wsMessageAddReceiver.setReciverTypeCode(aReciverTypeCode);
				wsMessageAddReceiver.setSendTime(null);
				wsMessageAddReceiver.setSendTypeCode(aSendTypeCode);
				result = stub.wsMessageAddReceiver(wsMessageAddReceiver).get_return();
				// System.out.println("添加接收人：" + result);
			}
			// // 发送
			UMessServiceStub.WsMessageSend wsMessageSend = new UMessServiceStub.WsMessageSend();
			wsMessageSend.setMessageId(messageId);
			result = stub.wsMessageSend(wsMessageSend).get_return();
			if(false == result.equals("1")) {
				Log.println("**发送失败：" + result);
			}

			// 登出
			//System.out.println("登出");
			stub.wsCsLogout(new UMessServiceStub.WsCsLogout());

			// 查询登录状态
			//System.out.print("登录状态：");
			//System.out.println(stub.wsCsCheckLogin(new UMessServiceStub.WsCsCheckLogin()).get_return());
			return true;
		} catch (RemoteException e) {
			Log.println(e);
			String to  ="";
			java.util.Iterator<String> iter = aAdressTo.iterator();
			while (iter.hasNext()) {
				to = to + iter.next();
			}
			Log.println("**detail：" + " AdressTo:" + aAdressTo + " Title:" + aTitle + " Content:" + aContent + " FromAddress:" + aFromAddress + " FromName:" + aFromName + " SendTypeCode:" + aSendTypeCode + " ReciverTypeCode:" + aReciverTypeCode);
			//e.printStackTrace();
			return false;
		} catch (Exception e) {
			Log.println(e);
			//e.printStackTrace();
			return false;
		} 
	}
}
